package com.etxxy.web.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.etxxy.member.domain.Member;
import com.etxxy.web.constance.WebConstance;
import com.etxxy.web.security.shrio.ShiroUtils;

/**
 * @category 基础控制器
 * @author yiz
 *
 */

public abstract class BaseController {
	public static final Logger logger = LoggerFactory.getLogger(BaseController.class);
	//得到session
	public Member getMemberFromSession(){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		Member member = (Member) session.getAttribute(WebConstance.USERSESSIONKEY);
		logger.info("member={}",JSON.toJSONString(member));
		return member;
	}
	
	//设置session
	public Member setSession(Member member){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		session.setAttribute(WebConstance.USERSESSIONKEY, member);
		logger.info("member={}",JSON.toJSONString(member));
		return member;
	}
	
	public String getLoginMemberId(){
		String memberId = ShiroUtils.getMemberId();
		if(StringUtils.isBlank(memberId)) {
			return null;
		} else {
			return memberId;
		}
	}
	
}
